//https://leetcode.cn/problems/linked-list-cycle-ii/
package codeRandomThoughts.Test142环形链表II;

import codeRandomThoughts.utils.ListNode;

public class Solution {

    /**
     * 很巧妙啊这个
     * @param head
     * @return
     */
    public ListNode detectCycle(ListNode head) {
        //定义快慢指针
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast==slow){
                ListNode meetPoint = fast;
                ListNode index = head;
                while (meetPoint!=index){
                    meetPoint=meetPoint.next;
                    index=index.next;
                }
                return index;
            }
        }
        return null;
    }
}
